package com.yzh.luckydraw.test.domain;

import com.alibaba.fastjson.JSON;
import com.yzh.luckydraw.common.Constants;
import com.yzh.luckydraw.domain.award.model.req.GoodsReq;
import com.yzh.luckydraw.domain.award.model.res.DistributionRes;
import com.yzh.luckydraw.domain.award.service.factory.DistributionGoodsFactory;
import com.yzh.luckydraw.domain.award.service.goods.IDistributionGoods;
import com.yzh.luckydraw.domain.strategy.model.req.DrawReq;
import com.yzh.luckydraw.domain.strategy.model.res.DrawResult;
import com.yzh.luckydraw.domain.strategy.model.vo.DrawAwardVO;
import com.yzh.luckydraw.domain.strategy.service.draw.IDrawExec;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DistributionGoods {
    private Logger logger = LoggerFactory.getLogger(DistributionGoods.class);
    @Resource
    private IDrawExec drawExec;
    @Resource
    private DistributionGoodsFactory distributionGoodsFactory;
    @Test
    public void test() {
        //抽奖
        DrawResult drawResult = drawExec.doDraw(new DrawReq("杨子豪", 10001L));
        //判断抽奖结果
        Integer drawState = drawResult.getDrawState();
        if (Constants.DrawState.FAIL.getCode().equals(drawState)) {
            logger.info("未中奖");
            return;
        }
        //封装发奖参数，orderId：2109313442431 为模拟ID，需要在用户参与领奖活动时生成
        DrawAwardVO drawAwardVO = drawResult.getDrawAwardInfo();
        GoodsReq req = new GoodsReq(drawResult.getuId(), 2109313442431L, drawAwardVO.getAwardId(), drawAwardVO.getAwardName(), drawAwardVO.getAwardContent());
        //根据awardType 从工厂中获取相应的发奖服务
        IDistributionGoods distributionGoodsService = distributionGoodsFactory.getDistributionGoodsService(drawAwardVO.getAwardType());
        DistributionRes distributionRes = distributionGoodsService.doDistribution(req);
        logger.info("测试结果：{}", JSON.toJSONString(distributionRes));
    }
}
